package com.heima.article.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.extension.conditions.update.LambdaUpdateChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.heima.article.dto.ArticleDto;
import com.heima.article.dto.ArticleHomeDto;
import com.heima.article.dto.ArticleInfoDto;
import com.heima.article.entity.ApArticle;
import com.heima.article.entity.ApArticleContent;
import com.heima.article.mapper.ApArticleMapper;
import com.heima.article.service.IApArticleContentService;
import com.heima.article.service.IApArticleService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.heima.article.service.ITemplateService;
import com.heima.common.dto.ResponseResult;
import com.heima.common.enums.AppHttpCodeEnum;
import com.heima.common.minio.MinIOService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Lazy;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * <p>
 * 文章信息表，存储已发布的文章 服务实现类
 * </p>
 *
 * @author mcm
 * @since 2022-08-10
 */
@Service
public class ApArticleServiceImpl extends ServiceImpl<ApArticleMapper, ApArticle> implements IApArticleService {

    @Autowired
    IApArticleContentService contentService;

    @Autowired
    @Lazy
    ITemplateService templateService;

    @Autowired
    MinIOService minIOService;
    //保存文章
    public ResponseResult<Long> saveArticle(ArticleDto dto) {
        ApArticle apArticle=new ApArticle();
        BeanUtils.copyProperties(dto,apArticle);
        //判断参数
        if (dto.getId()==null) {
            //id为空 ，新增
            apArticle.setCreatedTime(new Date());
            this.save(apArticle);
            //保存内容表
            ApArticleContent content=new ApArticleContent();
            content.setArticleId(apArticle.getId());
            content.setContent(dto.getContent());
            contentService.save(content);
            // 生成静态html  需要异步处理
            System.out.println(Thread.currentThread().getName() + " 开始调用生成静态html服务...");
            templateService.createHtml(apArticle, dto.getContent());
            System.out.println(Thread.currentThread().getName() + " 生成静态html调用结束");
            //返回通用相应
            return ResponseResult.okResult(apArticle.getId());
        }else{
            //id不为空,修改文章
            this.updateById(apArticle);
            //修改内容表
            LambdaUpdateWrapper<ApArticleContent> qw=new LambdaUpdateWrapper<>();
            qw.eq(ApArticleContent::getArticleId,dto.getId());
            qw.set(ApArticleContent::getContent,dto.getContent());
            contentService.update(qw);
            //删除之前的静态页面，重新生成
            if (!StringUtils.isEmpty(dto.getStaticUrl())) {
                minIOService.delete(dto.getStaticUrl());
                templateService.createHtml(apArticle, dto.getContent());
            }
            return ResponseResult.okResult(apArticle.getId());
        }
    }

    //加载文章   type:0: 默认首页最新文章  1: 更多文章-下一页
    public ResponseResult loadArticle(ArticleHomeDto dto, int type) {
        //构造查询条件
        LambdaQueryWrapper<ApArticle> qw=new LambdaQueryWrapper<>();
        //如果频道id不为0，添加频道查询条件
        if (dto.getChannelId()!=0) {
            qw.eq(ApArticle::getChannelId,dto.getChannelId());
        }
        //如果类型是0，加载最新文章 添加查询条件，发布时间大于页面传递的最大时间
        if (type==0) {
            qw.gt(ApArticle::getPublishTime,dto.getMaxTime());
        }
        //如果类型是1，加载最新文章 添加查询条件，发布时间小于页面传递的最大时间
        if (type==1) {
            qw.lt(ApArticle::getPublishTime,dto.getMinTime());
        }
        //已下架，和已删除的文章需要排除
        qw.eq(ApArticle::getIsDelete,false);
        qw.eq(ApArticle::getIsDown,false);
        //文章列表按照发布时间倒叙排序
        qw.orderByDesc(ApArticle::getPublishTime);
        //构造分页条件
        Page<ApArticle> page = new Page<>(1, dto.getSize());
        Page<ApArticle> page1 = this.page(page, qw);
        return ResponseResult.okResult(page1.getRecords());
    }

    //文章详情
    public ResponseResult loadArticleInfo(ArticleInfoDto dto) {
        //根据id查询文章表和文章内容表
        ApArticle article = this.getById(dto.getArticleId());
        LambdaQueryWrapper<ApArticleContent> qw=new LambdaQueryWrapper<>();
        qw.eq(ApArticleContent::getArticleId,dto.getArticleId());
        ApArticleContent content = contentService.getOne(qw);
        //构造相应对象
        Map<String,Object> map=new HashMap<>();
        map.put("config", article);
        map.put("content",content);
        return ResponseResult.okResult(map);
    }
}
